package com.lagou.edu.minicat.core;

import com.lagou.edu.minicat.base.LifecycleBase;
import lombok.Data;

import java.util.concurrent.*;

@Data
public class Server extends LifecycleBase {
    private Service service;

    private Connector connector;

    private Executor executor;

    public Server() {
        //定义线程池
        int corePoolSize = 10;
        int maximumPoolSize = 50;
        long keepAliveTime = 100L;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(50);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

        this.executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    @Override
    protected void initInternal() {
        service.init();
        connector.init();
    }

    @Override
    protected void startInternal() {
        service.start();
        connector.start();
    }
}
